home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0066_Complex Math Unit.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-25  |  2KB  |  86 lines

  1.  
  2. {Just for grins, here's a complex number unit I wrote come time back:}
  3.  
  4. unit complex;
  5. (*
  6.  polar/rectangular conversions and complex math
  7.  Steve Rogers, ~1993
  8. *)
  9.  
  10. {----------------------}
  11. interface
  12.  
  13. type
  14.   tComplex=record
  15.     r,             { real component }
  16.     x              { imaginary component }
  17.       : real;
  18.   end;
  19.  
  20. procedure r2p(var r,p : tComplex);
  21. procedure p2r(var p,r : tComplex);
  22. procedure c_add(var c1,c2,c3 : tComplex);
  23. procedure c_sub(var c1,c2,c3 : tComplex);
  24. procedure c_mult(var c1,c2,c3 : tComplex);
  25. procedure c_div(var c1,c2,c3 : tComplex);
  26.  
  27. implementation
  28.  
  29. const
  30.   RADS=0.0174532; { degree to radian conversion constant }
  31.  
  32. {----------------------}
  33. procedure r2p(var r,p : tComplex);
  34. { returns polar in degrees in p, given rectangular in r }
  35. begin
  36.   p.r:= sqrt(sqr(r.r)+sqr(r.x));
  37.   p.x:= arctan(r.x/r.r)/RADS;
  38. end;
  39.  
  40. {----------------------}
  41. procedure p2r(var p,r : tComplex);
  42. { returns rectangular in r, given polar in degrees in p }
  43. begin
  44.   r.r:= p.r*cos(p.x*RADS);
  45.   r.x:= p.r*sin(p.x*RADS);
  46. end;
  47.  
  48. {----------------------}
  49. procedure c_add(var c1,c2,c3 : tComplex);
  50. { adds c2 to c1, places result in c3 }
  51. begin
  52.   c3.r:= c1.r+c2.r;
  53.   c3.x:= c1.x+c2.x;
  54. end;
  55.  
  56. {----------------------}
  57. procedure c_sub(var c1,c2,c3 : tComplex);
  58. { subtracts c2 from c1, places result in c3 }
  59. begin
  60.   c3.r:= c1.r-c2.r;
  61.   c3.x:= c1.x-c2.x;
  62. end;
  63.  
  64. {----------------------}
  65. procedure c_mult(var c1,c2,c3 : tComplex);
  66. { multiplies c1 by c2, places result in c3  }
  67. begin
  68.   c3.r:= (c1.r*c2.r)-(c1.x*c2.x);
  69.   c3.x:= (c1.r*c2.x)+(c1.x*c2.r);
  70. end;
  71.  
  72. {----------------------}
  73. procedure c_div(var c1,c2,c3 : tComplex);
  74. { divides c1 by c2, places result in c3  }
  75. var
  76.   p1,p2,p3 : tComplex;
  77.  
  78. begin
  79.   r2p(c1,p1);                          { convert c1 to polar form }
  80.   r2p(c2,p2);                          { convert c2 to polar form }
  81.   p3.r:= p1.r/p2.r;                    { divide real component    }
  82.   p3.x:= p1.x-p2.x;                    { subtract imaginary component }
  83.   if (p3.x<0) then p3.x:= p3.x+180;    { Pretty it up                 }
  84.   p2r(p3,c3);                          { convert c3 back to rectangular }
  85. end;
  86.